//
//  ShoppingCarCell.swift
//  koolcar
//
//  Created by xiantian on 2023/11/3.
//

import UIKit

class ShoppingCarCell: UITableViewCell {
    var selectBtn: UIButton = {
        let tempBtn = UIButton()
        tempBtn.setup_image(image: UIImage.init(named: "icon_shopingcar_normal"))
        return tempBtn
    }()
    
    var imgView: UIImageView = {
        let tempImgView = UIImageView()
        tempImgView.contentMode = .scaleAspectFit
        tempImgView.backgroundColor = .lightGray
        return tempImgView
    }()
    
    var titleLab: UILabel = {
        let tempLab = UILabel()
        tempLab.textColor = Theme.titleLabColor
        tempLab.backgroundColor = UIColor.clear
        tempLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 14)
        tempLab.numberOfLines = 0
        tempLab.lineBreakMode = .byTruncatingTail
        tempLab.textAlignment = .left
        return tempLab
    }()
    
    var priceLab: UILabel = {
        let tempLab = UILabel()
        tempLab.textColor = Theme.titleLabColor
        tempLab.backgroundColor = UIColor.clear
        tempLab.font = UIFont.init(name: Theme.PingFangSCMedium, size: 14)
        tempLab.numberOfLines = 1
        tempLab.lineBreakMode = .byTruncatingTail
        tempLab.textAlignment = .left
        return tempLab
    }()
    
    var signLab: UILabel = {
        let tempLab = UILabel()
        tempLab.textColor = Theme.whiteColor
        tempLab.backgroundColor = UIColor.init(hex: 0xFED764)
        tempLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 11)
        tempLab.numberOfLines = 1
        tempLab.lineBreakMode = .byTruncatingTail
        tempLab.textAlignment = .center
        tempLab.layer.masksToBounds = true
        tempLab.layer.cornerRadius = 11
        return tempLab
    }()
    
    var numberView: CarNumberView = {
        let tempView = CarNumberView(frame: CGRect.init(x: 0, y: 0, width: 100, height: 34))
        return tempView
    }()
    
    var lineView: UIView = {
        let tempView = UIView()
        tempView.backgroundColor = Theme.lineColor
        tempView.isHidden = true
        return tempView
    }()
    
    var dataModel: ShoppingCarListModel!
    var timer: Timer!   //修改购物车数量延迟请求
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.isSkeletonable = true
        contentView.addSubview(selectBtn)
        contentView.addSubview(titleLab)
        contentView.addSubview(imgView)
        contentView.addSubview(signLab)
        contentView.addSubview(priceLab)
        contentView.addSubview(numberView)
        contentView.addSubview(lineView)
        
        selectBtn.addTarget(self, action: #selector(selectBtnAction), for: .touchUpInside)
        numberView.callBack = {[weak self] aValue in
            if self?.timer != nil {
                self?.timer.invalidate()
                self?.timer = nil
            }
            self?.timer = Timer.scheduledTimer(withTimeInterval: 0.6, block: { aTime in
                self?.dataModel.modifyBlock?(aValue)
            }, repeats: false)
            RunLoop.current.add(self!.timer, forMode: RunLoopMode.commonModes)
        }
        self.addSnpLayout()
    }
    
    private func addSnpLayout(){
        selectBtn.snp.makeConstraints { make in
            make.top.equalTo(14)
            make.left.equalTo(15)
            make.width.equalTo(30)
            make.height.equalTo(70)
        }
        
        self.imgView.snp.makeConstraints { (make) in
            make.left.equalTo(self.selectBtn.snp.right).offset(14)
            make.width.equalTo(66)
            make.height.equalTo(70)
            make.top.equalTo(14)
        }
        
        self.titleLab.snp.makeConstraints { make in
            make.left.equalTo(imgView.snp.right).offset(10)
            make.top.equalTo(14)
            make.right.equalTo(-14)
            make.height.greaterThanOrEqualTo(20)
        }
        
        signLab.snp.makeConstraints { make in
            make.left.equalTo(imgView.snp.right).offset(10)
            make.top.equalTo(titleLab.snp.bottom).offset(10)
            make.width.equalTo(38)
            make.height.equalTo(20)
        }
        
        priceLab.snp.makeConstraints { make in
            make.left.equalTo(signLab.snp.right).offset(10)
            make.top.equalTo(titleLab.snp.bottom).offset(10)
            make.right.equalTo(-14)
            make.height.greaterThanOrEqualTo(20)
        }
        
        numberView.snp.makeConstraints { make in
            make.right.equalTo(-14)
            make.top.equalTo(priceLab.snp.bottom).offset(10)
            make.height.equalTo(30)
            make.width.equalTo(100)
            make.bottom.lessThanOrEqualTo(-14)
        }
        
        lineView.snp.makeConstraints { make in
            make.left.equalTo(10)
            make.right.equalTo(-10)
            make.height.equalTo(0.5)
            make.bottom.equalTo(-0.5)
        }
    }
    ///discountType  1：会员折扣 2：商品折扣 3:限时折扣
    func setShoppingCarCell(carModel: ShoppingCarListModel){
        self.dataModel =  carModel
        let imagesArray = (carModel.productImages ?? "").components(separatedBy: ",")
        if imagesArray.count > 0, let tempUrl = URL.init(string: imagesArray[0]){
           self.imgView.sd_setImage(with: tempUrl, placeholderImage: UIImage(named: "icon_default_place"))
        }else{
            self.imgView.image = UIImage.init(named: "icon_default_place")
        }
        
        self.titleLab.text = dataModel.productName ?? ""
        let price = XTManageCenter.showPriceInComma(source: (carModel.price ?? 0).cleanZero)
        self.priceLab.text = String(format: "%@ %@", XTManageCenter.currentSign(), price)
       
        
        self.numberView.numValue = dataModel.quantity ?? 0
        self.numberView.maxNumber = carModel.availableStock ?? 0
        
        //标签
        self.signLab.textColor = .white
        self.signLab.backgroundColor = Theme.titleLabColor
        switch carModel.discountType ?? 0 {
            case 1:
                //会员折扣,显示VIP
                self.signLab.text = "VIP"
                self.signLab.textColor = .white
                self.signLab.backgroundColor = Theme.titleLabColor
            case 2:
                //商品折扣，显示特价
                self.signLab.text = MMLocalizedString("shopping_price_tip1", comment: "特价")
                self.signLab.textColor = Theme.whiteColor
                self.signLab.backgroundColor = UIColor.init(hex: 0xFED764)
            case 3:
                //限时折扣,显示限时
                self.signLab.text = MMLocalizedString("shopping_price_tip2", comment: "限时")
                self.signLab.textColor = Theme.whiteColor
                self.signLab.backgroundColor = UIColor.init(hex: 0xFED764)
            default:
                self.signLab.text = ""
        }
        if self.signLab.text != "" {
            self.signLab.isHidden = false
            self.signLab.sizeToFit()
            let sign_width = signLab.frame.size.width + 20
            self.signLab.snp.updateConstraints { make in
                make.width.equalTo(sign_width)
            }
            self.priceLab.snp.updateConstraints { make in
                make.left.equalTo(signLab.snp.right).offset(10)
            }
        }else{
            self.signLab.isHidden = true
            self.signLab.snp.updateConstraints { make in
                make.width.equalTo(0)
            }
            self.priceLab.snp.updateConstraints { make in
                make.left.equalTo(signLab.snp.right).offset(0)
            }
        }
        
        if carModel.isSelect {
            self.selectBtn.setup_image(image: UIImage.init(named: "icon_shopingcar_selected"))
        }else{
            self.selectBtn.setup_image(image: UIImage.init(named: "icon_shopingcar_normal"))
        }
    }
    
    ///勾选商品
    @objc func selectBtnAction(){
        self.dataModel.selectBlock?()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
